#include "Threadpool.h"
#include "TcpServer.h"
#include "MyTask.h"
#include <iostream>


using std::endl;
using std::cout;
Threadpool threadpool(4,10);

void onConnection(TcpConnectionPtr conn)
{
    cout << conn->toString() << " has connected" << endl;
}

void onMessage(TcpConnectionPtr conn)
{
    // cout << "main.cpp 18 onMessage(), in onMessage" << endl;
    string msg = conn->recv();
    // cout << "main.cpp 19 OnMessage(), msg = " << msg << endl;
    // shared_ptr<Task> task(new MyTask(msg, conn));
    Task *task = new MyTask(msg,conn);
    //交给线程来执行，当消息到来时
    threadpool.addTask(task);

}
void onClose(TcpConnectionPtr conn)
{
    cout << conn->toString() << " has disconnected" << endl;

}

int main(int argc, char *argv[])
{
    threadpool.start();

    TcpServer server("0.0.0.0",8080);
    server.setAllCallbacks(onClose, onMessage, onConnection);
    server.start();
    return 0;
}

